//
//  Effect.m
//  puzzle
//
//  Created by omega on 09/07/24.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Effect.h"
#import "touchHell.h"
#import "hellApp.h"
#import <OpenGLES/ES1/gl.h>

#define EFFECT_MAX				(128)

typedef struct{
	float x,y;
	float vx,vy;
	int timer;
	int param;
	Effect_Type type;
} Effect;

static const int effect_param_lifetime[] = { 0 , 120 , 120 , 120 };

Effect effect[EFFECT_MAX];



void GameState_updateEffect(){
	int t;
	static int timer = 0;
	timer++;
	for( t = 0 ; t < EFFECT_MAX ; t++ ){
		if( effect[t].type == EFFECT_TYPE_NONE ) continue;
		
		effect[t].timer++;
		if(effect[t].timer > effect_param_lifetime[ effect[t].type ]){
			effect[t].type = EFFECT_TYPE_NONE;
		}
		
		effect[t].x += effect[t].vx;
		effect[t].y += effect[t].vy;
		if( (timer + t) % 4 == 0 ){
			effect[t].vx *= 0.92;
			effect[t].vy *= 0.92;
			effect[t].vy -= 0.4;
		}
	}
}

void GameState_drawEffect(){
	int t;
	static int timer = 0;
	timer++;
	for( t = 0 ; t < EFFECT_MAX ; t++ ){
		if( effect[t].type == EFFECT_TYPE_NONE ) continue;
		
		glPushMatrix();		
		glTranslatef( effect[t].x , effect[t].y , 0 );
		switch ( effect[t].type ) {
			case EFFECT_TYPE_FLYBLOCK:
				Hell_setAlphaBlendMode( HELL_ALPHABLEND_NORMAL );
				glRotatef( effect[t].timer * 10 * ((t % 2)*2 - 1) ,0,0,1);
				//draw_panel( (int)effect[t].param , 255 );
				break;
			case EFFECT_TYPE_CROSS:
				if( timer < effect_param_lifetime[ effect[t].type ] - 80 || (timer + t) % 2 ) {
					Hell_setAlphaBlendMode( HELL_ALPHABLEND_ADD );
					glRotatef( timer * 10 ,0,0,1);
					Hell_drawTextureColor( HELL_TEXTURE_BOX ,  64 , 128 , 32 , 32 , rand()%255 , rand()%255 , rand()%255 , 255 );
				}
				break;
			default:
				break;
		}
		glPopMatrix();
	}
}



void GameState_addEffect( float x , float y , float vx, float vy , Effect_Type type , int param){
	static int ring_idx = 0;
	for(int t = 0 ; t < EFFECT_MAX ; t++ ){
		int idx = (t + ring_idx) % EFFECT_MAX;
		if( effect[idx].type != EFFECT_TYPE_NONE ) continue;
		
		effect[idx].type = type;
		effect[idx].x = x;
		effect[idx].y = y;
		effect[idx].vx = vx;
		effect[idx].vy = vy;
		effect[idx].timer = 0;
		effect[idx].param = param;
		
		ring_idx = (idx + 1) % EFFECT_MAX;
		return;
	}	
}